# -*- coding: utf-8 -*- 

#########################################################################
## This is a samples controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does streaming)
## - call exposes all registered services (none by default)
#########################################################################  
import time, datetime

def url(f, args=[]): return URL(r=request,f=f,args=args)

error_page=URL(r=request,f='error')

def about():
    """
    We need on of these for the liscense also
    All about this applications.
    Displays app_name/ABOUT
    """
    import os
    filename = os.path.join(request.env.gluon_parent,'applications',request.application, 'ABOUT')
    return response.render(dict(about=MARKMIN(open(filename,'r').read())))


def index():
    '''
    First we check to see is our site visitor is logged in. If
    so then we want to display the menu's appropriate for the 
    groups the user is a member of.
    '''
    if me != None:
        display_authenticated_user_menus()
    return dict(message=T('Hello World'))

def user():
    """
    exposes:
    http://..../[app]/default/user/login 
    http://..../[app]/default/user/logout
    http://..../[app]/default/user/register
    http://..../[app]/default/user/profile
    http://..../[app]/default/user/retrieve_password
    http://..../[app]/default/user/change_password
    use @auth.requires_login()
        @auth.requires_membership('group name')
        @auth.requires_permission('read','table name',record_id)
    to decorate functions that need access control
    """
    return dict(form=auth())


def download():
    """
    allows downloading of uploaded files
    http://..../[app]/default/download/[filename]
    """
    return response.download(request,db)


def call():
    """
    exposes services. for example:
    http://..../[app]/default/call/jsonrpc
    decorate with @services.jsonrpc the functions to expose
    supports xml, json, xmlrpc, jsonrpc, amfrpc, rss, csv
    """
    session.forget()
    return service()
